package model;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import  sun.audio.*;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import UI.Main;

public class EnemyManager  extends Thread implements MouseListener{

	private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
	
	private Player p;
	
	private Dimension screenDimensions;
	
	private Main main;
	
	//private URL u = getClass().getResource("/data/sound/shot.wav");
	private String u = "./data/sound/shot.wav";
	

	
	public EnemyManager(Player player, Main m){
		main = m;
		p = player;
		screenDimensions = m.getContentPane().getSize();
		for(int i = 0; i < 5; i++){
			Enemy e = new Enemy(screenDimensions);
			enemies.add(e);
			main.add(e.getLabel());
		}
	}
	
	public void advanceEnemies(){
		//main.repaintBG();
		for(Enemy e: enemies){
			e.advance(screenDimensions.width/2, screenDimensions.height/2);
			
			//e.getLabel().repaint();
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		Point origin = arg0.getPoint();
		Clip clip;
		try {
			clip = AudioSystem.getClip();
			//AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(u));
			AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource("/data/sound/shot.wav"));
	        clip.open(inputStream);
	        clip.start(); 
		} catch (LineUnavailableException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		try{
		for(Enemy e: enemies){
			if(e.contains(origin)){
				main.remove(e.getLabel());
				System.out.println("kill");
				p.setKillsLeft(p.getKillsLeft()-1);
				p.setScore(p.getScore()+5);
				enemies.remove(e);
			}
		}
		}catch(Exception e){}
		//System.out.println(origin);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void run(){
		while(p.getKillsLeft() > 0){
			advanceEnemies();
			if(enemies.size() < 8){
				Enemy e = new Enemy(screenDimensions);
				enemies.add(e);
				main.add(e.getLabel());
			}
			checkIfLose();
			try {
				Thread.sleep(350);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void checkIfLose(){
		int w = main.getContentPane().getSize().width;
		int h = main.getContentPane().getSize().height;
		Point[] puntos = {
				new Point(w/2+16,h/2+14),
				//new Point(w/2+33,h/2),
				//new Point(w/2,h/2+27),
				//new Point(w/2+33,h/2+27)
		};
		for(Point po:puntos){
			for(Enemy e: enemies){
				if(e.contains(po)){
					main.lose();
				}
			}
		}
		
	}
	
}
